package com.msb.dongbao.portal.web.controller.studyCaptcha;

import com.msb.dongbao.portal.web.code.ImageCode;
import com.msb.dongbao.portal.web.code.ValidCodeGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Map;

/**
 * @author: may
 * @date: 2021年05月27日 22:49
 */
@RestController
@RequestMapping("/code")
public class VerifyCodeController {
    String attrName = "verifyCode";

    @GetMapping("/generator-base64")
    public String generatorCode(HttpServletRequest request, HttpServletResponse response) {
        String base64Str = "";
        try {
            ImageCode imageCode = ImageCode.getInstance();

            // 验证码的值
            String code = imageCode.getCode();

            request.getSession().setAttribute(attrName, code);
            // 验证码图片
            ByteArrayInputStream image = imageCode.getImage();

            //验证码图片转base64字符串
            /*ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] bytes = new byte[1024];

            int r = 0;

            while ((r = image.read(bytes)) != -1) {
                baos.write(bytes, 0, r);
            }

            byte[] swap = baos.toByteArray();

            base64Str = Base64.getEncoder().encodeToString(swap);*/

            //通过输出流写到浏览器
            response.setContentType("image/jpeg");
            byte[] bytes = new byte[1024];
            try (ServletOutputStream out = response.getOutputStream()) {
                while (image.read(bytes) != -1) {
                    out.write(bytes);
                }
            }
        } catch (Exception e) {
            System.out.println("异常");
        }
        return base64Str;
    }

    @GetMapping("/verify")
    public String verify(String verifyCode, HttpServletRequest request) {

        String s = request.getSession().getAttribute(attrName).toString();
        if (verifyCode.equals(s)) {
            return "验证码校验通过";
        }

        return "验证码校验不通过";
    }

    @GetMapping("/vaildateCode")
    public void vaildateCode(HttpServletRequest req, HttpServletResponse resp, Integer type)
            throws IOException {
        final int width = 400;
        final int height = 150;
        final int codecount = 4;
        Map<String, Object> map = ValidCodeGenerator.generateCodeAndPic(width, height, codecount);
        // 将四位数字的验证码保存到Session中。
        /*HttpSession session = req.getSession();
        if (type == null || type == 0) {
            session.setAttribute("vaildateCode", map.get("code"));
        } else if (type == 1) {
            session.setAttribute("forgetVaildateCode", map.get("code"));
        }*/
        // 禁止图像缓存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
        resp.setContentType("image/jpeg");
        // 将图像输出到Servlet输出流中。
        ServletOutputStream sos = resp.getOutputStream();
        byte[] byte1 = (byte[]) map.get("codePic");
        ByteArrayInputStream in = new ByteArrayInputStream(byte1);    //将b作为输入流；
        BufferedImage image = ImageIO.read(in);
        ImageIO.write(image, "jpeg", sos);
    }
}
